//****************************************************************
//LinkedIterator.java       Authors: Lewis/Chase

//Represents an iterator for a linked list of linear nodes.
//****************************************************************

package jss2;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class DoubleLinkedIterator <T> implements Iterator <T> {

	private DoubleNode <T>	current;	// the current position

	//-------------------------------------------------------------
	//  Sets up this iterator using the specified items.
	//-------------------------------------------------------------
	public DoubleLinkedIterator (final DoubleNode <T> collection) {

		this.current = collection;
	}

	//-------------------------------------------------------------
	//  Returns true if this iterator has at least one more element
	//  to deliver in the iteration.
	//-------------------------------------------------------------
	public boolean hasNext () {

		return this.current != null;
	}

	public boolean hasPrevious () {

		return this.current != null;
	}

	//-------------------------------------------------------------
	//  Returns the next element in the iteration. If there are no
	//  more elements in this iteration, a NoSuchElementException is
	//  thrown.
	//-------------------------------------------------------------
	public T next () {

		if (!this.hasNext ())
			throw new NoSuchElementException ();

		final T result = this.current.getElement ();
		this.current = this.current.getNext ();
		return result;
	}

	public T previous () {

		if (!this.hasPrevious ())
			throw new NoSuchElementException ();

		final T result = this.current.getElement ();
		this.current = this.current.getPrevious ();
		return result;
	}

	//-------------------------------------------------------------
	//  The remove operation is not supported.
	//-------------------------------------------------------------
	public void remove () throws UnsupportedOperationException {

		throw new UnsupportedOperationException ();
	}
}
